/**
 * @author		Sergey Suzdalskiy
 * @e-mail:		sergey.suzdalskiy@gmail.com
 */
package ua.kiev.gcore.ui.controls.gallery
{
	import flash.display.Sprite;
	import flash.geom.Point;
	
	import ua.kiev.gcore.ui.controls.HBox;
	
	public class GalleryBox extends Sprite
	{
		//Align
		public static const VERTICAL_LEFT_TYPE:int = 1;
		public static const HORIZONTAL_LEFT_TYPE:int = 2;
		
		public static const HORIZONTAL_RIGHT_TYPE:int = 3;
		public static const VERTICAL_RIGHT_TYPE:int = 4;
		
		
		private var _itemsSeparator:Point;
		private var _size:Point;
		
		private var items:Vector.<GalleryItem>;
		
		private var type:int;
		
		public function GalleryBox(type:int = HORIZONTAL_LEFT_TYPE)
		{
			super();
			this.type = type;
			this._itemsSeparator = new Point(0, 0);
			this.items = new Vector.<GalleryItem>();
		}
		
		
		
		public function clear():void{
			while(numChildren){
				removeChildAt(0);
			}
		}
		
		public function get itemsSeparator():Point{
			return _itemsSeparator;
		}
		
		public function set itemsSeparator(value:Point):void{
			_itemsSeparator = value;
			draw();
		}
		
		public function get size():Point{
			return _size;
		}
		
		public function set size(value:Point):void{
			_size = value;
			draw();
		}
		
		public function set Items(value:Vector.<GalleryItem>):void{
			items = value;
			draw();
		}
		
		protected function draw():void{
			clear();
			switch(type){
				case VERTICAL_LEFT_TYPE:
					drawVL();
					break;
				case HORIZONTAL_LEFT_TYPE:
					drawHL();
					break;
				case VERTICAL_RIGHT_TYPE:
					drawVR();
					break;
				case HORIZONTAL_RIGHT_TYPE:
					drawHR();
					break;
				default:
					for each(var item:GalleryItem in items){
						addChild(item.sprite);
					}
					break;					
			}
		}
			
		protected function drawVL():void{
			var dX:int = itemsSeparator.x;
			var dY:int = itemsSeparator.y;
			
			for (var i:int = 0; i < items.length; i++){
				var item:GalleryItem = items[i];
				
				if((item.size.y + dY) > size.y){
					dY = itemsSeparator.y;
					dX += item.size.x + itemsSeparator.x;						
					if((item.size.x + dX) > size.x){							
						break;
					}
				}
				item.sprite.x = dX;
				item.sprite.y = dY;
				dY += item.size.y + itemsSeparator.y;
				
				addChild(item.sprite);
			}
		}
		
		protected function drawHL():void{
			var dX:int = itemsSeparator.x;
			var dY:int = itemsSeparator.y;
			
			for (var i:int = 0; i < items.length; i++){
				var item:GalleryItem = items[i];
				
				if((item.size.x + dX) > size.x){
					dX = itemsSeparator.x;
					dY += item.size.y + itemsSeparator.y;						
					if((item.size.y + dY) > size.y){							
						break;
					}
				}
				item.sprite.x = dX;
				item.sprite.y = dY;
				dX += item.size.x + itemsSeparator.x;
				
				addChild(item.sprite);
			}
		}
		
		protected function drawVR():void{
			var dX:int = itemsSeparator.x;
			var dY:int = itemsSeparator.y;
			
			for (var i:int = 0; i < items.length; i++){
				var item:GalleryItem = items[i];
				
				if((item.size.y + dY) > size.y){
					dY = itemsSeparator.y;
					dX += item.size.x + itemsSeparator.x;						
					if((item.size.x + dX) > size.x){							
						break;
					}
				}
				item.sprite.x = dX;
				item.sprite.y = dY;
				dY += item.size.y + itemsSeparator.y;
				
				addChild(item.sprite);
			}
		}
		
		protected function drawHR():void{
			var dX:int = size.x;
			var dY:int = size.y;
			
			for (var i:int = 0; i < items.length; i++){
				var item:GalleryItem = items[i];
				
				if((dX - item.size.x) < 0){
					dX = size.x;
					dY -= (item.size.y + itemsSeparator.y);						
					if((dY - item.size.y) < 0){							
						break;
					}
				}
				item.sprite.x = dX - item.size.x;
				item.sprite.y = dY - item.size.y;
				dX -= (item.size.x + itemsSeparator.x);
				
				addChild(item.sprite);
			}
		}
		
	}
}